﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Homework_Helper
{
    public partial class Form1 : Form
    {
        int questionNumber;

        bool lastWasWrong;

        List<string> question;
        List<string> answear;
        List<bool> done;

        int right;
        int wrong;

        public Form1(string[] args)
        {
            InitializeComponent();

            scrambleQuestionsToolStripMenuItem.Checked = Properties.Settings.Default.ScrambleQuestions;
            caseSensitiveToolStripMenuItem.Checked = Properties.Settings.Default.CaseSensitive;

            foreach(string arg in args)
                MessageBox.Show(arg);

            if (args.Length != 0)
                OpenFile(args[0]);

            newHomeworkToolStripMenuItem.Click += new EventHandler(NewHomework);
            openHomeworkToolStripMenuItem.Click += new EventHandler(OpenHomework);
            saveHomeworkToolStripMenuItem.Click += new EventHandler(SaveHomework);
            nextQuestionToolStripMenuItem.Click += new EventHandler(NextQuestion);
            Ok.Click += new EventHandler(NextQuestion);

            scrambleQuestionsToolStripMenuItem.CheckedChanged += new EventHandler(ChangeSettings);
            caseSensitiveToolStripMenuItem.CheckedChanged += new EventHandler(ChangeSettings);
        }

        public void NewHomework(object o, EventArgs e)
        {
            lastWasWrong = false;
            question = new List<string>();
            answear = new List<string>();
            done = new List<bool>();

            while (true)
            {
                NewQuestion form = new NewQuestion();
                form.ShowDialog();

                if (form.question == "" || form.answear == "")
                    break;

                question.Add(form.question);
                answear.Add(form.answear);
                done.Add(false);
            }

            SetQuestion(true);

            this.Text = "Homework Helper";
        }

        public void OpenHomework(object o, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();

            if (result == DialogResult.Cancel)
                return;

            OpenFile(openFileDialog1.FileName);
        }

        public void OpenFile(string filename)
        {
            lastWasWrong = false;
            question = new List<string>();
            answear = new List<string>();
            done = new List<bool>();

            StreamReader sr = new StreamReader(filename);

            while (!sr.EndOfStream)
            {
                string Question = sr.ReadLine();
                string Answear = sr.ReadLine();

                if (Question == "" || Answear == "")
                    break;

                question.Add(Question);
                answear.Add(Answear);
                done.Add(false);

                sr.ReadLine();
            }

            sr.Close();

            SetQuestion(true);

            this.Text = "Homework Helper - " + Path.GetFileName(openFileDialog1.FileName);
        }

        public void SaveHomework(object o, EventArgs e)
        {
            DialogResult result = saveFileDialog1.ShowDialog();

            if (result == DialogResult.Cancel)
                return;

            StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
            int at = 0;
            foreach (string quest in question)
            {
                sw.WriteLine(question[at]);
                sw.WriteLine(answear[at]);
                sw.WriteLine();

                at++;
            }

            sw.Close();

            this.Text = "Homework Helper - " + Path.GetFileName(saveFileDialog1.FileName);
        }

        public void NextQuestion(object o, EventArgs e)
        {
            if (question == null || answear == null)
                return;

            bool scramble = scrambleQuestionsToolStripMenuItem.Checked;

            if (String.Compare(Answear.Text, answear[questionNumber], !caseSensitiveToolStripMenuItem.Checked) == 0)
            {
                MessageBox.Show("Right!");
                if (!lastWasWrong)
                    right++;
                lastWasWrong = false;
                done[questionNumber] = true;
                questionNumber++;
            }
            else
            {
                MessageBox.Show("Wrong!\n" + answear[questionNumber]);
                if (!lastWasWrong)
                    wrong++;
                lastWasWrong = true;
                scramble = false;
            }

            int dones = 0;
            foreach (bool isDone in done)
                if (isDone)
                    dones++;

            if (dones == question.Count)
            {
                MessageBox.Show("Right: " + right + "\n" +
                                "Wrong: " + wrong + "\n" +
                                "Out of: " + question.Count);
                scramble = false;
                questionNumber = 0;

                right = 0;
                wrong = 0;
            }
            SetQuestion(scramble);
        }

        public void SetQuestion(bool scramble)
        {
            if (scramble)
            {
                int questionID = -1;
                for (int quest = new Random().Next(question.Count); quest < question.Count; quest++)
                {
                    if (!done[quest])
                    {
                        questionID = quest;
                        break;
                    }
                }
                if (questionID != -1)
                    questionNumber = questionID;
                else
                {
                    for (int quest = 0; quest < question.Count; quest++)
                    {
                        if (!done[quest])
                        {
                            questionID = quest;
                            break;
                        }
                    }
                }
                if (questionID != -1)
                    questionNumber = questionID;
            }
            Question.Text = question[questionNumber];
            Answear.Text = "";

            Answear.Focus();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox about = new AboutBox();
            about.ShowDialog();
        }

        private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string website = "http://homeworkhelper.codeplex.com/documentation";
            try
            {
                Process.Start(website);
            }
            catch
            {
                MessageBox.Show("The website couldn't be opened, please visit the following site:\n" +
                website, "Info!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ChangeSettings(object o, EventArgs e)
        {
            Properties.Settings.Default.ScrambleQuestions = scrambleQuestionsToolStripMenuItem.Checked;
            Properties.Settings.Default.CaseSensitive = caseSensitiveToolStripMenuItem.Checked;
            Properties.Settings.Default.Save();
        }
    }
}
